package com.kfm.dao.impl;

import com.kfm.dao.UserInfoMapper;
import com.kfm.model.KfmUser;
import com.kfm.model.Userinfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class UserInfoMapperImpl implements UserInfoMapper {

    @Override
    public List<Userinfo> selectAll() {
        // 执行查询的操作
        try {
            // 1. 加载配置文件
            // UserInfoDaoImpl.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            // 2. 创建SqlSessionFactory构建者对象
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            // 3. 通过配置文件创建SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = builder.build(in);
            // 4. 通过SqlSessionFactory 创建SqlSession对象
            SqlSession sqlSession = sqlSessionFactory.openSession();
            // 5. 通过SqlSession对象执行sql
            // Statement 命名空间 + id --> statement
            return sqlSession.selectList("com.kfm.dao.UserInfoMapper.selectAll");
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public List<KfmUser> selectAllKfm() {
        try {
            InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory sqlSessionFactory = builder.build(in);
            SqlSession sqlSession = sqlSessionFactory.openSession();
            return sqlSession.selectList("com.kfm.dao.UserInfoMapper.selectAllKfm");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
